#ifndef _CONN_MANAGER_HPP_20120407
#define _CONN_MANAGER_HPP_20120407

#include <ext/hash_map>

#include "object_pool.hpp"
#include "base.hpp"
#include "sock_addr.hpp"
#include "idatagram_factory.hpp"
#include "simple_epoll.hpp"
#include "iprocessor.hpp"

using namespace datagram;

class FixedSizeAllocator;

namespace tcp
{
	class Connection;
	class SimpleEpoll;
	
	class ConnManager
	{
		public :
			typedef __gnu_cxx::hash_map<uint32_t, Connection*> ConnMap;
			typedef ConnMap::iterator ConnMap_Iter;
			typedef ConnMap::const_iterator ConnMap_CIter;

			typedef ObjectPool<Connection> ConnPool;

		public :
			ConnManager(int max_conn, FixedSizeAllocator * alloc, 
					long epoll_timeout = EPOLL_TIMEOUT);
			~ConnManager();

			Connection * allocate(const SockAddr & addr, int fd);
			void         deallocate(Connection * conn);

			int add_acceptor(const char * host, int port,
					IDatagramFactory * factory, IProcessor * processor);
			
			int add_connector(const char * host, int port, 
					IDatagramFactory * factory, IProcessor * processor);

			int poll();

		public :
			long                 _epoll_timeout;

			SimpleEpoll          _simple_epoll;
			
			FixedSizeAllocator * _alloc;

			ConnPool             _conn_pool;	
			
			ConnMap              _conn_map;

			const static long    EPOLL_TIMEOUT = 10;
			const static unsigned int EPOLL_EVENT = EPOLLIN | EPOLLOUT | EPOLLET;

		private :
			uint32_t             _conn_id;

		private :
			uint32_t     next_id() { return ++ _conn_id; }
	};
};

#endif

